package ui.activity.homepage;

import adapter.DispatchRiderAdapter;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import base.BaseBean;
import base.activity.BaseLayoutActivity;
import bean.DispatchRiderBean;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.dispatchapp.andlei.R;
import netWork.NetUtils;
import utils.GsonUtils;
import widget.ClearEditText;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 分配骑手页面
 *
 * @author Andlei
 * @DATE 2019/1/28
 */
public class DispatchRiderActivity extends BaseLayoutActivity {
    private ClearEditText clearEditText;
    private RecyclerView recyclerView;
    private TextView tv_jiedan;
    private RelativeLayout rlayout_xiugai;
    private Button btn_suresend;
    private DispatchRiderAdapter adapter;
    private List<DispatchRiderBean> list = new ArrayList<>();
    private NetUtils netUtils;
    private String order_no;
    private String rid;
    private String addr_id;
    private String homepage, type, order_num;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("", true);
        setTitlefontBg_black();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_dispatch_rider;
    }

    @Override
    protected void findViews() {
        netUtils = new NetUtils(mActivity, netRequestCallBack);
        clearEditText = getView(R.id.edittext_search);
        recyclerView = getView(R.id.list_rcy);
        rlayout_xiugai = getView(R.id.rlayout_xiugai);
        tv_jiedan = getView(R.id.tv_jiedan);
        btn_suresend = getView(R.id.btn_suresend);
    }

    @Override
    protected void formatViews() {
        btn_suresend.setOnClickListener(this);
        if (!TextUtils.isEmpty(homepage)) {
            tv_jiedan.setText("已选" + order_num + "单");
        } else {
            tv_jiedan.setText("已选1单");
        }
    }

    @Override
    protected void formatData() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(mActivity);
        layoutManager.setOrientation(LinearLayout.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new DispatchRiderAdapter(R.layout.item_dispatch_rider, list);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (DispatchRiderBean riderBean : list) {
                    riderBean.setSelect(false);
                }
                list.get(position).setSelect(true);
                if (!TextUtils.isEmpty(homepage)) {
                    tv_jiedan.setText("已选" + order_num + "单"+ "      " + list.get(position).getNickname());
                } else {
                    tv_jiedan.setText("已选1单"+ "      " + list.get(position).getNickname());
                }
                adapter.notifyDataSetChanged();
            }
        });
        if (!TextUtils.isEmpty(homepage)) {
            Map<String, Object> map = new HashMap<>();
            map.put("addr_id", "" +addr_id);
            netUtils.get("rider/user/school-rider", map, BaseBean.class, null, false);
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("order_no", "" + order_no);
            netUtils.get("rider/user/available-rider", map, BaseBean.class, null, false);
        }

    }

    @Override
    protected void getBundle(Bundle bundle) {
        order_no = bundle.getString("order_no");
        homepage = bundle.getString("homepage");
        addr_id = bundle.getString("addr_id");
        order_num = bundle.getString("order_num");
        type = bundle.getString("type");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_suresend:
                dispatchride();
                break;
        }
    }

    private void dispatchride() {
        if (!TextUtils.isEmpty(homepage)) {
            Map<String, Object> map = new HashMap<>();
            map.put("order_nos", "" + order_no);
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).isSelect()) {
                    rid = list.get(i).getRid();
                }
            }
            map.put("rid", "" + rid);
            if(TextUtils.isEmpty(rid)){
                toast("请分配骑手");
                return;
            }
            netUtils.post("rider/user/distribute", map, BaseBean.class, false);
        } else {
            Map<String, Object> map = new HashMap<>();
            map.put("order_nos", "" + order_no);
            map.put("type", type);
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).isSelect()) {
                    rid = list.get(i).getRid();
                }
            }
            map.put("rid", "" + rid);
            if(TextUtils.isEmpty(rid)){
                toast("请分配骑手");
                return;
            }
            netUtils.post("rider/user/redistribute", map, BaseBean.class, false);
        }

    }

    private NetUtils.NetRequestCallBack netRequestCallBack = new NetUtils.NetRequestCallBack() {
        @Override
        public void error(String action, Throwable e, Map tag) {

        }

        @Override
        public void success(String action, BaseBean t, Map tag) {
            if (t.getCode() == 200) {
                if (action.equals("rider/user/available-rider")) {
                    list = GsonUtils.getInstance(mActivity).parseNoHeaderJArray(t, DispatchRiderBean.class);
                    adapter.setNewData(list);
                } else if (action.equals("rider/user/school-rider")) {
                    list = GsonUtils.getInstance(mActivity).parseNoHeaderJArray(t, DispatchRiderBean.class);
                    adapter.setNewData(list);
                } else if (action.equals("rider/user/distribute")) {
                    setResult(100);
                    finish();
                } else if (action.equals("rider/user/redistribute")) {
                    setResult(100);
                    finish();
                }
            } else {
                if (action.equals("rider/user/available-rider")||action.equals("rider/user/school-rider")) {
                    finish();
                }
                toast(t.getMsg());
            }
        }
    };
}
